package com.ruoyi.custom.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.ruoyi.common.constant.CustomConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.TCustomInfoMapper;
import com.ruoyi.custom.domain.TCustomInfo;
import com.ruoyi.custom.service.ITCustomInfoService;

/**
 * 客户信息Service业务层处理
 * 
 * @author wzy
 * @date 2025-01-03 09:15:59
 */
@Service
public class TCustomInfoServiceImpl implements ITCustomInfoService 
{
    @Autowired
    private TCustomInfoMapper tCustomInfoMapper;

    /**
     * 查询客户信息
     * 
     * @param customerId 客户信息主键
     * @return 客户信息
     */
    @Override
    public TCustomInfo selectTCustomInfoByCustomerId(Long customerId)
    {
        return tCustomInfoMapper.selectTCustomInfoByCustomerId(customerId);
    }

    /**
     * 查询客户信息列表
     * 
     * @param tCustomInfo 客户信息
     * @return 客户信息
     */
    @Override
    public List<TCustomInfo> selectTCustomInfoList(TCustomInfo tCustomInfo)
    {
        return tCustomInfoMapper.selectTCustomInfoList(tCustomInfo);
    }

    /**
     * 新增客户信息
     * 
     * @param tCustomInfo 客户信息
     * @return 结果
     */
    @Override
    public int insertTCustomInfo(TCustomInfo tCustomInfo)
    {
        tCustomInfo.setCreateTime(DateUtils.getNowDate());
        return tCustomInfoMapper.insertTCustomInfo(tCustomInfo);
    }

    /**
     * 修改客户信息
     * 
     * @param tCustomInfo 客户信息
     * @return 结果
     */
    @Override
    public int updateTCustomInfo(TCustomInfo tCustomInfo)
    {
        tCustomInfo.setUpdateTime(DateUtils.getNowDate());
        return tCustomInfoMapper.updateTCustomInfo(tCustomInfo);
    }

    /**
     * 批量删除客户信息
     * 
     * @param customerIds 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteTCustomInfoByCustomerIds(Long[] customerIds)
    {
        return tCustomInfoMapper.deleteTCustomInfoByCustomerIds(customerIds);
    }

    /**
     * 删除客户信息信息
     * 
     * @param customerId 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteTCustomInfoByCustomerId(Long customerId)
    {
        return tCustomInfoMapper.deleteTCustomInfoByCustomerId(customerId);
    }

    /**
     * 查询所有客户信息
     *
     * @return 客户信息
     */
    @Override
    public List<TCustomInfo> selectTCustomInfoAll()
    {
        return tCustomInfoMapper.selectTCustomInfoAll();
    }

    /**
     * 根据用户ID获取客户选择框列表
     *
     * @param userId 用户ID
     * @return 选中客户ID列表
     */
    @Override
    public List<Long> selectCustomListByUserId(Long userId)
    {
        return tCustomInfoMapper.selectCustomListByUserId(userId);
    }

    /**
     * 根据用户ID获取数据权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectCustomPermissionByUserId(Long userId)
    {
        List<String> perms = tCustomInfoMapper.selectCustomPermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    /**
     * 校验客户名称是否唯一
     *
     * @param tCustomInfo 客户信息
     * @return 结果
     */
    @Override
    public String checkCustomerNameUnique(TCustomInfo tCustomInfo)
    {
        Long customerId = StringUtils.isNull(tCustomInfo.getCustomerId()) ? -1L : tCustomInfo.getCustomerId();
        TCustomInfo info = tCustomInfoMapper.checkCustomerNameUnique(tCustomInfo.getCustomerName());
        if (StringUtils.isNotNull(info) && info.getCustomerId().longValue() != customerId.longValue())
        {
            return CustomConstants.NOT_UNIQUE;
        }
        return CustomConstants.UNIQUE;
    }

}
